import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:website.core/core/application/blocs/app_navigator_bloc/models/page_configs/account_page_config.dart';
import 'package:website.core/core/application/blocs/app_navigator_bloc/models/page_configs/app_navigator_page_config.dart';
import 'package:website.core/core/application/blocs/app_navigator_bloc/models/page_configs/documentation_page_config.dart';
import 'package:website.core/core/application/blocs/app_navigator_bloc/models/page_configs/faq_page_config.dart';
import 'package:website.core/core/application/blocs/app_navigator_bloc/models/page_configs/home_page_config.dart';
import 'package:website.core/core/application/blocs/app_navigator_bloc/models/page_configs/signing_in_page_config.dart';
import 'package:website.core/core/application/blocs/app_navigator_bloc/models/page_configs/signing_up_page_config.dart';
import 'package:website.core/core/application/blocs/app_navigator_bloc/models/page_configs/web_purchase_item_page_config.dart';

class AppRouteInformationParser extends RouteInformationParser<AppNavigatorPageConfig> {
  @override
  Future<AppNavigatorPageConfig> parseRouteInformation(RouteInformation routeInformation) async {
    final uri = Uri.parse(routeInformation.location ?? '');

    if (uri.pathSegments.length == 0) {
      const pageConfig = HomePageConfig();
      return SynchronousFuture(pageConfig);
    }

    if (uri.pathSegments.length == 1 && uri.pathSegments[0] == 'faq') {
      const pageConfig = FaqPageConfig();
      return SynchronousFuture(pageConfig);
    }

    if (uri.pathSegments.length == 1 && uri.pathSegments[0] == 'docs') {
      const pageConfig = DocumentationPageConfig();
      return SynchronousFuture(pageConfig);
    }

    if (uri.pathSegments.length == 1 && uri.pathSegments[0] == 'sign-up') {
      const pageConfig = SigningUpPageConfig();
      return SynchronousFuture(pageConfig);
    }

    if (uri.pathSegments.length == 1 && uri.pathSegments[0] == 'sign-in') {
      const pageConfig = SigningInPageConfig();
      return SynchronousFuture(pageConfig);
    }

    if (uri.pathSegments.length == 1 && uri.pathSegments[0] == 'premium') {
      const pageConfig = WebPurchaseItemPageConfig();
      return SynchronousFuture(pageConfig);
    }

    if (uri.pathSegments[0] == 'account') {
      if (uri.pathSegments.length == 2 && uri.pathSegments[1] == 'overview') {
        const fragmentConfig = AccountOverviewAccountPageFragmentConfig();

        const pageConfig = AccountPageConfig(
          fragmentConfig: fragmentConfig,
        );

        return SynchronousFuture(pageConfig);
      }

      if (uri.pathSegments.length == 2 && uri.pathSegments[1] == 'purchase-management') {
        const fragmentConfig = PurchaseManagementAccountPageFragmentConfig();

        const pageConfig = AccountPageConfig(
          fragmentConfig: fragmentConfig,
        );

        return SynchronousFuture(pageConfig);
      }

      if (uri.pathSegments.length == 2 && uri.pathSegments[1] == 'transaction-history') {
        const fragmentConfig = TransactionHistoryAccountPageFragmentConfig();

        const pageConfig = AccountPageConfig(
          fragmentConfig: fragmentConfig,
        );

        return SynchronousFuture(pageConfig);
      }

      if (uri.pathSegments.length == 2 && uri.pathSegments[1] == 'payment-methods') {
        const fragmentConfig = PaymentMethodAccountPageFragmentConfig();

        const pageConfig = AccountPageConfig(
          fragmentConfig: fragmentConfig,
        );

        return SynchronousFuture(pageConfig);
      }

      const fragmentConfig = AccountOverviewAccountPageFragmentConfig();

      const pageConfig = AccountPageConfig(
        fragmentConfig: fragmentConfig,
      );

      return SynchronousFuture(pageConfig);
    }

    const pageConfig = HomePageConfig();
    return SynchronousFuture(pageConfig);
  }

  @override
  RouteInformation? restoreRouteInformation(AppNavigatorPageConfig pageConfig) {
    if (pageConfig is HomePageConfig) {
      return const RouteInformation(location: '/');
    }

    if (pageConfig is FaqPageConfig) {
      return const RouteInformation(location: '/faq');
    }

    if (pageConfig is DocumentationPageConfig) {
      return const RouteInformation(location: '/docs');
    }

    if (pageConfig is SigningUpPageConfig) {
      return const RouteInformation(location: '/sign-up');
    }

    if (pageConfig is SigningInPageConfig) {
      return const RouteInformation(location: '/sign-in');
    }

    if (pageConfig is WebPurchaseItemPageConfig) {
      return const RouteInformation(location: '/premium');
    }

    if (pageConfig is AccountPageConfig) {
      final fragmentConfig = pageConfig.fragmentConfig;

      if (fragmentConfig is AccountOverviewAccountPageFragmentConfig) {
        return const RouteInformation(location: '/account/overview');
      }

      if (fragmentConfig is PurchaseManagementAccountPageFragmentConfig) {
        return const RouteInformation(location: '/account/purchase-management');
      }

      if (fragmentConfig is TransactionHistoryAccountPageFragmentConfig) {
        return const RouteInformation(location: '/account/transaction-history');
      }

      if (fragmentConfig is PaymentMethodAccountPageFragmentConfig) {
        return const RouteInformation(location: '/account/payment-methods');
      }

      throw UnimplementedError();
    }

    throw UnimplementedError();
  }
}
